package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class loginController {

    @Autowired
    private JedisPool jedisPool;

    @Reference(version = "1.0.0")
    private MemberService memberService;

    @PostMapping("/check")
    public Result check(@RequestBody Map<String, String> map, HttpServletResponse httpServletResponse) {

        String telephone = map.get("telephone");
        String validateCode = map.get("validateCode");

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String codeStr = jedis.get("LOGIN" + telephone);
            if (codeStr == null) {
                return Result.fail(-999, "验证码已失效");
            } else {
                if (!codeStr.equals(validateCode)) {
                    return Result.fail(-999, "验证码不正确");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(-999, "系统异常，登录失败");
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        Member member = memberService.findByTelephone(telephone);

        if (member == null) {
            Member newMember = new Member();
            newMember.setPhoneNumber(telephone);
            newMember.setRegTime(new Date());
            Integer save = memberService.save(newMember);
            newMember.setId(save);
            try {
                String jsonString = JSON.toJSONString(newMember);
                jedis = jedisPool.getResource();
                jedis.setex("SESSION" + telephone, 30 * 60, jsonString);
                Cookie cookie = new Cookie("telephone", telephone);
                cookie.setPath("/");
                cookie.setMaxAge(40 * 60);
                httpServletResponse.addCookie(cookie);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.fail(-999, "系统异常");
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
            return Result.success(newMember);

        } else {
            try {
                String jsonString = JSON.toJSONString(member);
                jedis = jedisPool.getResource();
                jedis.setex("SESSION" + telephone, 30 * 60, jsonString);
                Cookie cookie = new Cookie("telephone", telephone);
                cookie.setPath("/");
                cookie.setMaxAge(40 * 60);
                httpServletResponse.addCookie(cookie);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.fail(-999, "系统异常");

            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
            return Result.success(member);
        }
    }

}
